package it.patamau.bukkit.afk;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

public class AfkPlayerStats {
	
	//map name and player stat for all the playing characters (needed)
	private HashMap<String, AfkPlayerStat> afkplayers = new HashMap<String, AfkPlayerStat>();
	//list of afk players (name keys only)
	private ArrayList<String> afklist = new ArrayList<String>();
	
	public AfkPlayerStat get(String name){
		AfkPlayerStat p = afkplayers.get(name);
		if(p==null){
			p = new AfkPlayerStat(name,System.currentTimeMillis(),null);
			afkplayers.put(name, p);
		}
		return p;
	}
	
	public void setAfk(AfkPlayerStat pstat, boolean afk, long time, String reason){
		pstat.setAfk(afk);
		pstat.setTime(time);
		pstat.setReason(reason);
		if(afk){
			if(!afklist.contains(pstat.getName())){
				afklist.add(pstat.getName());
			}
		}else{
			afklist.remove(pstat.getName());
		}
	}
	
	public Collection<String> getAfkPlayers(){
		return afklist;
	}
	
	public Collection<AfkPlayerStat> getPlayers(){
		return afkplayers.values();
	}
	
	public void remove(String name){
		afklist.remove(name);
		afkplayers.remove(name);
	}
	
	public void clear(){
		afkplayers.clear();
		afklist.clear();
	}

}
